﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

/* Hannu Sirén */

namespace OudotOliotSMLiigassa
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BLLogic logic;
        private List<int> IDIndex;
        private Boolean updating;


        public MainWindow()
        {
            InitializeComponent();
            init();
        }

        private void init()
        {
            logic = new BLLogic();
            IDIndex = new List<int>();
            updating = false;
            
            for (int i = 0; i < logic.getLeagueSize(); i++) 
            {
                cbLeague.Items.Add(logic.getLeagues(i));
            }
            
            cbLeague.SelectedIndex = 0;
            lblInformation.Content = "Valmiina.";
            
        }

        private void updateIDIndex()
        {
            List<int> temp = new List<int>();
            IDIndex.RemoveAt(lbInfo.SelectedIndex);

            for (int i = 0; i < IDIndex.Count; i++)
            {
                temp.Add(IDIndex[i]);
            }

            IDIndex = temp;
        }

        private void updateLB()
        {
            updating = true;
            lbInfo.Items.Clear();

            for (int i = 0; i < IDIndex.Count; i++)
            {
                string fName = logic.getFNameByID(IDIndex[i]);
                string lName = logic.getLNameByID(IDIndex[i]);
                string league = logic.getLeagueByID(IDIndex[i]);

                lbInfo.Items.Add(lName + " " + fName + ", " + league);
            }
            updating = false;
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            if (logic.addPlayer(tbFirstName.Text, tbLastName.Text, cbLeague.SelectedValue.ToString(), tbPrice.Text))
            {
                IDIndex.Add(logic.getCurrentID());
                lbInfo.Items.Add(tbLastName.Text + " " + tbFirstName.Text + ", " + cbLeague.SelectedValue.ToString());
                lblInformation.Content = tbFirstName.Text + " " + tbLastName.Text + " luotu.";
                return;
            }
            lblInformation.Content = "Pelaajan luonti epäonnistui.";

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (lbInfo.SelectedItem == null)
            {
                lblInformation.Content = "Pelaajaa ei valittu. Ei muutosta.";
                return;
            }

            if (!logic.validate(tbFirstName.Text,
                tbLastName.Text, tbPrice.Text.ToString()))
            {
                lblInformation.Content = "Pelaajan tiedot ei kelpaa.";
                return;
            } 

            decimal price;
            if (!decimal.TryParse(tbPrice.Text, out price))
            {
                lblInformation.Content = "Pelaajan tiedot ei kelpaa.";
                return;
            }

            logic.setFNameByID(IDIndex[lbInfo.SelectedIndex], tbFirstName.Text);
            logic.setLNameByID(IDIndex[lbInfo.SelectedIndex], tbLastName.Text);
            logic.setPriceByID(IDIndex[lbInfo.SelectedIndex], price);
            logic.setLeagueByID(IDIndex[lbInfo.SelectedIndex], cbLeague.SelectedValue.ToString());

            lblInformation.Content = "Pelaajan tiedot vaihdettu.";

            updateLB();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (lbInfo.SelectedItem == null)
            {
                lblInformation.Content = "Pelaajaa ei valittu. Ei muutosta.";
                return;
            }
            logic.removePlayer(IDIndex[lbInfo.SelectedIndex]);
            lblInformation.Content = "Pelaaja poistettu.";
            updateIDIndex();
            updateLB();
        }

        private void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            if (logic.saveFile())
            {
                lblInformation.Content = "Tiedot tallennettu tiedostoon SM_liiga.txt.";
                return;
            }
            lblInformation.Content = "Tallennus epäonnistui.";

        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void cbLeague_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void lbInfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (updating) return;
            tbFirstName.Text = logic.getFNameByID(IDIndex[lbInfo.SelectedIndex]);
            tbLastName.Text = logic.getLNameByID(IDIndex[lbInfo.SelectedIndex]);
            tbPrice.Text = logic.getPriceByID(IDIndex[lbInfo.SelectedIndex]).ToString();
            cbLeague.SelectedValue = logic.getLeagueByID(IDIndex[lbInfo.SelectedIndex]);

            lblInformation.Content = "Pelaaja valittu.";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void tbInformation_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
